package com.spring.arch.common.sourcegenerator;


import com.spring.arch.common.sourcegenerator.impl.ControllerGenerator;
import com.spring.arch.common.sourcegenerator.impl.DtoGenerator;
import com.spring.arch.common.sourcegenerator.impl.*;
import com.spring.arch.common.sourcegenerator.utils.Configuration;

public class DefaultGeneratorStarter {
    protected final String basePackage;
    protected final String[] models;
    protected final String outputMode;
    protected final boolean override;


    public DefaultGeneratorStarter(final String basePackage, final String outputMode, final boolean override, final String... models) {
        this.basePackage = basePackage;
        this.outputMode = outputMode;
        this.override = override;
        this.models = models;
    }

    public void generate() throws ClassNotFoundException {
        for (final String modelClass : this.models) {
            final Class<?> modelClazz = Class.forName(this.basePackage + "." + modelClass);
            final Configuration config = new Configuration(modelClazz, this.outputMode, null, this.override);
            final RepositoryGenerator repositoryGenerator = new RepositoryGenerator(config);
            final ServiceGenerator serviceGenerator = new ServiceGenerator(config);
            final DtoGenerator dtoGenerator = new DtoGenerator(config);
            final ConverterGenerator ConverterGenerator = new ConverterGenerator(config);
            final ControllerGenerator controllerGenerator = new ControllerGenerator(config);
            repositoryGenerator.generate();
            serviceGenerator.generate();
            dtoGenerator.generate();
            ConverterGenerator.generate();
            controllerGenerator.generate();
        }
    }

    public static boolean usageCheck(final String[] args) {
        if (args.length < 2) {
            System.out.println("请按照如下方式配置启动参数:");
            System.out.println("1. Model包路径: modelBasePackage=com.spring.arch.base.web.sourcegenerator.model");
            System.out.println("2. Model数组，逗号分隔: models=User,Member,Role");
            System.out.println("3. 输出方式，默认控制台打印， file则输出到文件: output=file");
            System.out.println("4. 是否覆盖原文件，输出方式为file时才生效。默认不覆盖原文件: overrid=true");
            System.out.println("*****************参考配置*******************");
            System.out.println("modelBasePackage=com.spring.arch.base.web.sourcegenerator.model");
            System.out.println("models=User,Member,Role");
            System.out.println("output=file");
            System.out.println("override=true");
            System.out.println("*****************参考配置*******************");
            return false;
        }
        return true;
    }

    public static void main(final String[] args) throws ClassNotFoundException {
        if (!DefaultGeneratorStarter.usageCheck(args)) {
            return;
        }

        final String basePackage = args[0].trim().replace("modelBasePackage=", "");
        final String[] models = args[1].trim().replace("models=", "").split(",");
        String outputMode = "console";
        if (args.length >= 3) {
            outputMode = args[2].trim().replace("output=", "").trim();
            if (!"file".equals(outputMode)) {
                outputMode = "console";
            }
        }
        boolean override = false;
        if ("file".equals(outputMode) && (args.length >= 4)) {
            final String overrideCommand = args[3].trim().replace("override=", "").trim();
            if ("true".equals(overrideCommand)) {
                override = true;
            }
        }

        final DefaultGeneratorStarter starter = new DefaultGeneratorStarter(basePackage, outputMode, override, models);
        starter.generate();
    }
}
